<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>删除订单详细</title>
  <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha1/dist/css/bootstrap.min.css" rel="stylesheet">
  <style>
    body {
      font-family: Arial, sans-serif;
      background-color: #FFEEF7;
      margin: 0;
      padding: 20px;
      color: #333;
    }
    .container {
      max-width: 600px;
      margin: 0 auto;
      background: #fff;
      padding: 20px;
      border-radius: 8px;
      box-shadow: 0 2px 5px rgba(0,0,0,0.2);
    }
    .form-group {
      margin-bottom: 10px;
    }
    label {
      display: block;
      margin-bottom: 5px;
    }
    input, select {
      width: 100%;
      padding: 8px;
      border: 1px solid #ccc;
      border-radius: 4px;
    }
    button {
      padding: 10px 20px;
      cursor: pointer;
      background-color: #28a745;
      color: white;
      border: none;
      border-radius: 4px;
    }
    button:hover {
      background-color: #4CAF50;
    }
    .delete-button {
      margin-top: 5px;
    }
  </style>
</head>
<body>
<div class="container">
  <h2>订单详细信息</h2>
  <div id="orderDetails">
    <!-- 订单详细信息将动态填充 -->
  </div>
  <button type="button" class="btn btn-danger" onclick="deleteAllOrderDetails()">确认删除所有订单详细</button>
</div>

<script>
  document.addEventListener('DOMContentLoaded', function() {
    // 从URL参数中获取传递的orderID
    const urlParams = new URLSearchParams(window.location.search);
    const orderId = urlParams.get('id');
    if (orderId) {
      fetchOrderDetailsByOrderId(orderId);
    } else {
      alert('未提供订单ID');
    }
  });

  function fetchOrderDetailsByOrderId(orderId) {
    fetch(`/api/orders/${orderId}/details`)
            .then(response => {
              if (!response.ok) {
                throw new Error('Network response was not ok');
              }
              return response.json();
            })
            .then(orderDetails => {
              const orderDetailsContainer = document.getElementById('orderDetails');
              orderDetailsContainer.innerHTML = ''; // Clear existing order details
              if (orderDetails.length === 0) {
                orderDetailsContainer.innerHTML = '<p>No order details found.</p>';
                return;
              }
              orderDetailsContainer.dataset.orderid = orderId;
              orderDetails.forEach(orderDetail => {
                const div = document.createElement('div');
                div.innerHTML = `
            <p>Order Detail ID: ${orderDetail.orderDetailID}, Product Name: ${orderDetail.product.name}, Quantity: ${orderDetail.quantity}, Subtotal: ${orderDetail.subtotal}</p>
            <button type="button" class="btn btn-danger delete-button" onclick="deleteOrderDetail(${orderDetail.orderDetailID})">删除这个订单详细</button>
          `;
                orderDetailsContainer.appendChild(div);
              });
            })
            .catch(error => {
              console.error('Error fetching order details:', error);
              alert('无法加载订单详情数据');
            });
  }

  function deleteOrderDetail(orderDetailId) {
    if (!confirm('Are you sure you want to delete this order detail?')) {
      return;
    }
    fetch(`/api/orderdetails/${orderDetailId}`, {
      method: 'DELETE',
    })
            .then(response => {
              if (!response.ok) {
                throw new Error('Network response was not ok');
              }
              return response.json();
            })
            .then(data => {
              console.log('Success:', data);
              alert('订单详细删除成功');
              fetchOrderDetailsByOrderId(document.getElementById('orderDetails').dataset.orderid);
            })
            .catch(error => {
              console.error('Error:', error);
              alert('订单详细删除失败: ' + error.message);
            });
  }

  function deleteAllOrderDetails() {
    if (!confirm('Are you sure you want to delete all order details?')) {
      return;
    }
    const orderId = document.getElementById('orderDetails').dataset.orderid;
    fetch(`/api/orders/${orderId}`, {
      method: 'DELETE',
    })
            .then(response => {
              if (!response.ok) {
                throw new Error('Network response was not ok');
              }
              return response.json();
            })
            .then(data => {
              console.log('Success:', data);
              alert('所有订单详细删除成功');
              window.history.back();
            })
            .catch(error => {
              console.error('Error:', error);
              alert('所有订单详细删除失败: ' + error.message);
            });
  }
</script>

</body>
</html>